package service

import (
	"RocFamilySystem/internal/repository"
	"RocFamilySystem/models"
	"RocFamilySystem/tools/excel"
	"github.com/xuri/excelize/v2"
	"log"
	"mime/multipart"
)

type HardwareDriverService struct {
	HardwareDriverRepository *repository.HardwareDriverRepository
}

// 初始化
func init() {
	registerContainer["HardwareDriverService"] = &HardwareDriverService{}
}

// SetRepository 设置持久层
func (service *HardwareDriverService) SetRepository(hardwareDriverRepository *repository.HardwareDriverRepository) {
	service.HardwareDriverRepository = hardwareDriverRepository
}

// AddHardwareDriver 添加硬件设备
func (service *HardwareDriverService) AddHardwareDriver(hardwareDriver *models.HardwareDriver) {
	service.HardwareDriverRepository.InsertHardwareDriver(hardwareDriver)
}

// RemoveHardwareDriver 删除硬件设备
func (service *HardwareDriverService) RemoveHardwareDriver(hardwareDriverId int) {
	service.HardwareDriverRepository.DeleteHardwareDriver(hardwareDriverId)
}

// ModifyHardwareDriver 修改硬件设备
func (service *HardwareDriverService) ModifyHardwareDriver(hardwareDriver *models.HardwareDriver) {
	service.HardwareDriverRepository.UpdateHardwareDriver(hardwareDriver)
}

// QueryHardwareDriver 查询硬件设备
func (service *HardwareDriverService) QueryHardwareDriver(hardwareDriver *models.HardwareDriver, pageNum, pageSize int) PageInfo {
	tables, total := service.HardwareDriverRepository.SelectHardwareDriver(hardwareDriver, pageNum, pageSize)
	return toPageInfo(pageNum, pageSize, total, tables)
}

// QueryHardwareDriverById 通过ID查询硬件设备
func (service *HardwareDriverService) QueryHardwareDriverById(id int) models.HardwareDriver {
	return service.HardwareDriverRepository.SelectHardwareDriverById(id)
}

// RemoveHardwareDriverBatch 批量删除硬件设备
func (service *HardwareDriverService) RemoveHardwareDriverBatch(ids []string) {
	service.HardwareDriverRepository.DeleteHardwareDriverBatch(ids)
}

// 工作簿名称
var hardwareDriverSheetName = "硬件设备"

// 属性位置信息以及顺序
var hardwareDriverPositions = []excel.ValuePosition{
	{"A", "SerialName", "设备名称"},
	{"B", "State", "使用状态"},
	{"C", "SerialNumber", "设备编号"},
	{"D", "Remark", "备注"},
	{"E", "CreateTime", "创建时间"},
}

// HardwareDriverTemplateDownload 硬件设备模板下载
func (service *HardwareDriverService) HardwareDriverTemplateDownload() *excelize.File {
	file := excelize.NewFile()
	excel.GeneratorHeader(file, hardwareDriverSheetName, hardwareDriverPositions)
	return file
}

// AddHardwareDriverBatch 批量添加硬件设备
func (service *HardwareDriverService) AddHardwareDriverBatch(uploadFile *multipart.FileHeader) {
	file, _ := uploadFile.Open()
	defer file.Close()
	excelFile, err := excelize.OpenReader(file)
	if err != nil {
		log.Panicln("模板文件错误！", err.Error())
	}
	var list []models.HardwareDriver
	excel.ReadExcel(excelFile, hardwareDriverSheetName, hardwareDriverPositions, 2, &list)
	service.HardwareDriverRepository.InsertHardwareDriverBatch(list)
}

// HardwareDriverExport 导出硬件设备
func (service *HardwareDriverService) HardwareDriverExport(ids []string) *excelize.File {
	list := service.HardwareDriverRepository.SelectHardwareDriverByIds(ids)
	file := excelize.NewFile()
	excel.GeneratorHeader(file, hardwareDriverSheetName, hardwareDriverPositions)
	excel.WriterExcel(file, hardwareDriverSheetName, hardwareDriverPositions, 2, &list)
	return file
}
